<template>
  <div class="home">
    <el-container class="con-conrainer">
      <el-header>
        <div class="home_logo">
          <img src="../assets/logo.png" alt="" />
          <span>电商后台管路系统</span>
        </div>
        <el-button class="btn-out" size="mini" type="primary" @click="out">退出</el-button>
      </el-header>
      <!--侧边栏 -->
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <el-menu background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
            <div class="toggle-button" @click="toggleCollapse">|||</div>
            <el-submenu :index="item.id + ''" v-for="item in menusLists" :key="item.id">
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveActivePath(subItem.path)">
                  <i class="el-icon-user"></i>
                  <span> {{ subItem.authName }}</span>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-container>
          <!-- 主页面区-->
          <el-main>
            <!-- 路由占位符 -->
            <router-view></router-view></el-main>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      menusLists: [],
      //菜单图标
      iconsObj: {
        '125': 'el-icon-user',
        '103': 'el-icon-unlock',
        '101': 'el-icon-shopping-cart-full',
        '102': 'el-icon-thumb',
        '145': 'el-icon-office-building'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  methods: {
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    //高亮设置
    saveActivePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    getmenus() {
      this.$http
        .get('menus')
        .then(res => {
         // console.log(res.data) //meta
          if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)

          this.menusLists = res.data.data
        })
        .catch(function(error) {
          // 请求失败处理
          console.log(error)
        })
    },
    out() {
      window.sessionStorage.clear()
      console.log('清除成功')
      this.$router.push('/login')
      this.$message.success('退出')
    }
  },
  created() {
    this.getmenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>
<style lang="less" scoped>
.home {
  width: 100%;
  height: 100%;
  background: #5555;
}
.con-conrainer {
  height: 100%;
}
.el-header {
  background-color: #b3c0d1;
  color: #fff;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

.home_logo img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #2d2d2d;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
